import request from '../utls/require'

// 挑战相关接口

/**
 * 获取关卡列表
 */
export const getLevelList = () => {
  console.log('[API] [Challenge] 开始发送获取关卡列表请求');
  return request({
    url: '/xiangsu/challenge/levels/',
    method: 'get'
  }).then(response => {
    console.log('[API] [Challenge] 获取关卡列表响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 获取关卡列表请求失败:', error);
    throw error;
  });
}

/**
 * 获取指定关卡的题目详情
 * @param levelId 关卡ID
 */
export const getLevelDetail = (levelId: number) => {
  console.log(`[API] [Challenge] 开始发送获取关卡${levelId}详情请求`);
  return request({
    url: `/xiangsu/challenge/levels/${levelId}/`,
    method: 'get'
  }).then(response => {
    console.log(`[API] [Challenge] 获取关卡${levelId}详情响应成功:`, response);
    return response;
  }).catch(error => {
    console.error(`[API] [Challenge] 获取关卡${levelId}详情请求失败:`, error);
    throw error;
  });
}

/**
 * 获取单个题目详情
 * @param questionId 题目ID
 */
export const getQuestionDetail = (questionId: number) => {
  console.log(`[API] [Challenge] 开始发送获取题目${questionId}详情请求`);
  return request({
    url: `/xiangsu/challenge/questions/${questionId}/`,
    method: 'get'
  }).then(response => {
    console.log(`[API] [Challenge] 获取题目${questionId}详情响应成功:`, response);
    return response;
  }).catch(error => {
    console.error(`[API] [Challenge] 获取题目${questionId}详情请求失败:`, error);
    throw error;
  });
}

/**
 * 获取随机题目
 */
export const getRandomQuestion = () => {
  console.log(`[API] [Challenge] 开始发送获取随机题目请求`);
  return request({
    url: '/xiangsu/challenge/questions/random/',
    method: 'get'
  }).then(response => {
    console.log('[API] [Challenge] 获取随机题目响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 获取随机题目请求失败:', error);
    throw error;
  });
}

/**
 * 提交答题答案
 * @param questionId 题目ID
 * @param selectedOptionId 选择的选项ID
 */
export const submitAnswer = (questionId: number, selectedOptionId: number) => {
  console.log(`[API] [Challenge] 开始发送提交答案请求: questionId=${questionId}, selectedOptionId=${selectedOptionId}`);
  return request({
    url: '/xiangsu/challenge/submit/',
    method: 'post',
    data: {
      question_id: questionId,
      selected_option_id: selectedOptionId
    }
  }).then(response => {
    console.log('[API] [Challenge] 提交答案响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 提交答案请求失败:', error);
    throw error;
  });
}

/**
 * 获取用户总体答题进度
 */
export const getUserProgress = () => {
  console.log('[API] [Challenge] 开始发送获取用户总体答题进度请求');
  return request({
    url: '/xiangsu/challenge/progress/',
    method: 'get'
  }).then(response => {
    console.log('[API] [Challenge] 获取用户总体答题进度响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 获取用户总体答题进度请求失败:', error);
    throw error;
  });
}

/**
 * 获取用户指定关卡的详细答题进度
 * @param levelId 关卡ID
 */
export const getLevelProgress = (levelId: number) => {
  console.log(`[API] [Challenge] 开始发送获取关卡${levelId}详细答题进度请求`);
  return request({
    url: `/xiangsu/challenge/progress/${levelId}/`,
    method: 'get'
  }).then(response => {
    console.log(`[API] [Challenge] 获取关卡${levelId}详细答题进度响应成功:`, response);
    return response;
  }).catch(error => {
    console.error(`[API] [Challenge] 获取关卡${levelId}详细答题进度请求失败:`, error);
    throw error;
  });
}

/**
 * 获取用户答题统计信息
 */
export const getUserStats = () => {
  console.log('[API] [Challenge] 开始发送获取用户答题统计信息请求');
  return request({
    url: '/xiangsu/challenge/stats/',
    method: 'get'
  }).then(response => {
    console.log('[API] [Challenge] 获取用户答题统计信息响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 获取用户答题统计信息请求失败:', error);
    throw error;
  });
}

/**
 * 获取排行榜
 * @param limit 返回前N名，默认10
 * @param period 时间范围，可选值：week/month/total，默认week
 */
export const getLeaderboard = (limit: number = 10, period: string = 'week') => {
  console.log(`[API] [Challenge] 开始发送获取排行榜请求: limit=${limit}, period=${period}`);
  return request({
    url: '/xiangsu/challenge/leaderboard/',
    method: 'get',
    params: {
      limit: limit,
      period: period
    }
  }).then(response => {
    console.log('[API] [Challenge] 获取排行榜响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 获取排行榜请求失败:', error);
    throw error;
  });
}

/**
 * 获取当前用户排名信息
 * @param period 时间范围，可选值：week/month/total，默认week
 */
export const getMyRank = (period: string = 'week') => {
  console.log(`[API] [Challenge] 开始发送获取当前用户排名信息请求: period=${period}`);
  return request({
    url: '/xiangsu/challenge/my_rank/',
    method: 'get',
    params: {
      period: period
    }
  }).then(response => {
    console.log('[API] [Challenge] 获取当前用户排名信息响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Challenge] 获取当前用户排名信息请求失败:', error);
    throw error;
  });
}

// 开发计划中提到但后端尚未实现的接口
// 这些接口可能需要后端进一步开发

/**
 * 获取指定关卡的题目（随机5题）
 * @param levelId 关卡ID
 * 
 * 注意：此接口在开发计划中提到，但后端当前未实现
 * 后端可能需要添加新的视图函数来支持此功能
 */
export const getLevelQuestions = (levelId: number) => {
  console.log(`[API] [Challenge] 开始发送获取关卡${levelId}随机题目请求`);
  // 这里可能需要后端添加新的接口，暂时使用关卡详情接口作为替代
  return getLevelDetail(levelId);
}